# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_pyname=Sphinx
_realname=sphinx
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.0.2
pkgrel=1
pkgdesc="Python documentation generator (mingw-w64)"
arch=('any')
license=('BSD')
url="https://www.sphinx-doc.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python-babel"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-docutils"
         "${MINGW_PACKAGE_PREFIX}-python-imagesize"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-pygments"
         "${MINGW_PACKAGE_PREFIX}-python-requests"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-snowballstemmer"
         "${MINGW_PACKAGE_PREFIX}-python-sphinx-alabaster-theme"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-applehelp"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-devhelp"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-htmlhelp"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-jsmath"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-serializinghtml"
         "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-qthelp")
optdepends=("${MINGW_PACKAGE_PREFIX}-imagemagick: ext.imageconverter")
install=${_realname}3-${CARCH}.install
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
source=("${_pyname}-${pkgver}.tar.gz"::"https://pypi.org/packages/source/S/${_pyname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('d7c6e72c6aa229caf96af82f60a0d286a1521d42496c226fe37f5a75dcfe2941')
noextract=(${_pyname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_pyname}-${pkgver} ]] && rm -rf ${srcdir}/${_pyname}-${pkgver}
  tar -xzf ${srcdir}/${_pyname}-${pkgver}.tar.gz -C ${srcdir} || true

  rm -rf python-build-${CARCH} | true
  cp -r "${_pyname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  #2 failed, 1165 passed, 21 skipped, 3 xfailed, 30 xpassed, 3 warnings
  ${MINGW_PREFIX}/bin/py.test || warning "test failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
}
